<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>C语言基础 - 循环结构</title>
    <link rel="stylesheet" href="common.css">
    <style>
        /* 页面特有样式 */
        .loop-card {
            background-color: #f5f9ff;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 25px;
            border-left: 4px solid #5D9CEC;
        }
        
        .flow-diagram {
            background-color: #f8f8f8;
            padding: 20px;
            border-radius: 5px;
            text-align: center;
            margin: 15px 0;
            font-family: monospace;
            overflow-x: auto;
        }
        
        .control-flow {
            background-color: #fff3e0;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
        }
        
        .nested-example {
            background-color: #e8f5e9;
            padding: 15px;
            border-radius: 5px;
        }
        
        .best-practice {
            background-color: #e3f2fd;
            padding: 12px;
            border-left: 4px solid #2196F3;
            margin: 15px 0;
        }
        
        .warning-note {
            background-color: #ffebee;
            border-left: 4px solid #f44336;
            padding: 12px;
            margin: 15px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>C语言基础入门课程</h1>
        </header>
        
        <nav>
            <a href="index.html">首页</a>
            <a href="02-intro.html">C语言简介与环境搭建</a>
            <a href="03-structure.html" class="current">C程序结构</a>
            <a href="04-datatypes.html">基本数据类型</a>
            <a href="05-variables.html">变量与常量</a>
            <a href="06-operators.html">运算符</a>
            <a href="07-io.html" class="current">输入输出</a>
            <a href="08-conditionals.html">条件语句</a>
            <a href="09-loops.html">循环结构</a>
            <a href="10-functions.html" class="current">函数基础</a>
            <a href="11-scope.html">作用域与存储类</a>
            <a href="12-recursion.html">递归函数</a>
            <a href="13-arrays.html">一维数组</a>
            <a href="14-multidim.html" class="current">多维数组</a>
            <a href="15-strings.html">字符串处理</a>
            <a href="16-pointers.html">指针基础</a>
            <a href="17-pointers_advanced.html" class="current">指针进阶</a>
            <a href="18-structs.html">结构体</a>
            <a href="19-files.html">文件操作</a>
            <a href="20-project.html">综合项目</a>
        </nav>
        
        <main>
            <h2>C语言循环结构</h2>
            
            <section>
                <h3>循环结构概述</h3>
                <p>循环结构允许程序重复执行某段代码，直到满足特定条件为止。</p>
            </section>
            
            <div class="loop-card">
                <h3>1. while循环</h3>
                
                <h4>基本语法</h4>
                <pre><code>while (条件表达式) {
    // 循环体代码
    // 需要改变条件变量，避免无限循环
}</code></pre>
                
                <div class="flow-diagram">
                    <h4>while循环流程图</h4>
                    <pre>
      +-----------+
      | 条件判断  |
      +-----+-----+
            |
   false +---+---+ true
          |       |
          | +-----+-----+
          | | 循环体代码 |
          | +-----+-----+
          |       |
          +-------+
                    </pre>
                </div>
                
                <h4>示例：计算1到100的和</h4>
                <pre><code>int sum = 0, i = 1;
while (i <= 100) {
    sum += i;
    i++;
}
printf("1到100的和是: %d\n", sum);</code></pre>
                
                <div class="best-practice">
                    <h4>使用场景</h4>
                    <ul>
                        <li>当循环次数不确定时</li>
                        <li>需要先判断条件再执行循环体时</li>
                        <li>读取输入直到特定值出现</li>
                    </ul>
                </div>
            </div>
            
            <div class="loop-card">
                <h3>2. do-while循环</h3>
                
                <h4>基本语法</h4>
                <pre><code>do {
    // 循环体代码
    // 需要改变条件变量
} while (条件表达式);</code></pre>
                
                <div class="flow-diagram">
                    <h4>do-while循环流程图</h4>
                    <pre>
      +-----------+
      | 循环体代码 |
      +-----+-----+
            |
   false +---+---+ true
          |       |
          +-------+
                    </pre>
                </div>
                
                <h4>示例：菜单选择</h4>
                <pre><code>char choice;
do {
    printf("1. 开始游戏\n");
    printf("2. 设置\n");
    printf("3. 退出\n");
    printf("请选择: ");
    scanf(" %c", &choice);
} while (choice != '1' && choice != '2' && choice != '3');</code></pre>
                
                <div class="warning-note">
                    <h4>与while循环的区别</h4>
                    <ul>
                        <li>do-while至少执行一次循环体</li>
                        <li>while可能一次都不执行</li>
                        <li>do-while末尾有分号</li>
                    </ul>
                </div>
            </div>
            
            <div class="loop-card">
                <h3>3. for循环</h3>
                
                <h4>基本语法</h4>
                <pre><code>for (初始化; 条件表达式; 更新) {
    // 循环体代码
}</code></pre>
                
                <div class="flow-diagram">
                    <h4>for循环流程图</h4>
                    <pre>
      +-----------+
      | 初始化    |
      +-----+-----+
            |
      +-----+-----+
      | 条件判断  |
      +-----+-----+
            |
   false +---+---+ true
          |       |
          | +-----+-----+
          | | 循环体代码 |
          | +-----+-----+
          |       |
          | +-----+-----+
          | | 更新表达式 |
          | +-----+-----+
          |       |
          +-------+
                    </pre>
                </div>
                
                <h4>示例：打印乘法表</h4>
                <pre><code>for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= i; j++) {
        printf("%d×%d=%-2d ", j, i, i*j);
    }
    printf("\n");
}</code></pre>
                
                <div class="best-practice">
                    <h4>使用场景</h4>
                    <ul>
                        <li>循环次数已知时</li>
                        <li>需要明确的初始化、条件和更新步骤时</li>
                        <li>遍历数组或集合</li>
                    </ul>
                </div>
            </div>
            
            <div class="control-flow">
                <h3>4. break与continue</h3>
                
                <h4>break语句</h4>
                <p>立即终止当前循环，执行循环后的代码。</p>
                <pre><code>// 查找第一个能被3和5整除的数
for (int i = 1; i <= 100; i++) {
    if (i % 3 == 0 && i % 5 == 0) {
        printf("找到: %d\n", i);
        break;  // 找到后立即退出循环
    }
}</code></pre>
                
                <h4>continue语句</h4>
                <p>跳过当前迭代，继续下一次循环。</p>
                <pre><code>// 打印1-100的奇数
for (int i = 1; i <= 100; i++) {
    if (i % 2 == 0) {
        continue;  // 跳过偶数
    }
    printf("%d ", i);
}</code></pre>
                
                <div class="warning-note">
                    <h4>注意事项</h4>
                    <ul>
                        <li>break只能跳出当前一层循环</li>
                        <li>continue在for循环中仍会执行更新表达式</li>
                        <li>避免过度使用，可能降低代码可读性</li>
                    </ul>
                </div>
            </div>
            
            <div class="nested-example">
                <h3>5. 循环嵌套</h3>
                
                <h4>基本概念</h4>
                <p>在一个循环体内包含另一个完整的循环结构。</p>
                
                <h4>示例：打印金字塔</h4>
                <pre><code>int rows = 5;
for (int i = 1; i <= rows; i++) {
    // 打印空格
    for (int j = 1; j <= rows - i; j++) {
        printf(" ");
    }
    // 打印星号
    for (int k = 1; k <= 2*i-1; k++) {
        printf("*");
    }
    printf("\n");
}</code></pre>
                
                <div class="best-practice">
                    <h4>嵌套循环建议</h4>
                    <ul>
                        <li>使用有意义的循环变量名(i,j,k等)</li>
                        <li>避免超过3层嵌套，考虑使用函数重构</li>
                        <li>内层循环应适当缩进</li>
                        <li>注意循环变量的作用域</li>
                    </ul>
                </div>
                
                <h4>综合示例：素数判断</h4>
                <pre><code>#include &lt;stdio.h&gt;
#include &lt;stdbool.h&gt;

int main() {
    int n;
    printf("请输入一个正整数: ");
    scanf("%d", &n);
    
    bool isPrime = true;
    for (int i = 2; i <= n/2; i++) {
        if (n % i == 0) {
            isPrime = false;
            break;
        }
    }
    
    if (isPrime && n > 1) {
        printf("%d是素数\n", n);
    } else {
        printf("%d不是素数\n", n);
    }
    
    return 0;
}</code></pre>
            </div>
        </main>
        

</body>
</html>